package com.adam.example.aggregation.metric;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.StatsAggregate;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import com.adam.example.ElasticsearchClientHolder;

import java.io.IOException;

public class StatsAggregationExample {

    public static void main(String[] args) throws IOException {
        ElasticsearchClient client = ElasticsearchClientHolder.getINSTANCE().initializeAndGetClient();

        SearchResponse<Void> searchResponse = client.search(builder -> builder
                        .index("test_books")
                        .size(0)
                        .aggregations("agg", aggregationBuilder -> aggregationBuilder
                                .stats(statsAggregationBuilder -> statsAggregationBuilder
                                        .field("price")
                                )
                        )
                , Void.class
        );
        Aggregate aggregate = searchResponse.aggregations().get("agg");
        StatsAggregate statsAggregate = aggregate.stats();
        System.out.println("max=" + statsAggregate.max());
        System.out.println("min=" + statsAggregate.min());
        System.out.println("sum=" + statsAggregate.sum());
        System.out.println("avg=" + statsAggregate.avg());
        System.out.println("count=" + statsAggregate.count());

        ElasticsearchClientHolder.getINSTANCE().close();
    }

}
